package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.WxOperateType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysWxOperateRecord;
import com.ruoyi.system.domain.SysWxapplyRecord;
import com.ruoyi.system.mapper.SysWxOperateRecordMapper;
import com.ruoyi.system.mapper.SysWxapplyRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWxGroupMapper;
import com.ruoyi.system.domain.SysWxGroup;
import com.ruoyi.system.service.ISysWxGroupService;

/**
 * wx群维护Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-27
 */
@Service
public class SysWxGroupServiceImpl implements ISysWxGroupService 
{
    @Autowired
    private SysWxGroupMapper sysWxGroupMapper;
    @Autowired
    private SysWxOperateRecordMapper sysWxOperateRecordMapper;
    @Autowired
    private SysWxapplyRecordMapper sysWxapplyRecordMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 查询wx群维护
     * 
     * @param id wx群维护主键
     * @return wx群维护
     */
    @Override
    public SysWxGroup selectSysWxGroupById(Long id)
    {
        return sysWxGroupMapper.selectSysWxGroupById(id);
    }

    /**
     * 查询wx群维护列表
     * 
     * @param sysWxGroup wx群维护
     * @return wx群维护
     */
    @Override
    public List<SysWxGroup> selectSysWxGroupList(SysWxGroup sysWxGroup)
    {
        return sysWxGroupMapper.selectSysWxGroupList(sysWxGroup);
    }

    /**
     * 新增wx群维护
     * 
     * @param sysWxGroup wx群维护
     * @return 结果
     */
    @Override
    public int insertSysWxGroup(SysWxGroup sysWxGroup)
    {
        sysWxGroup.setCreateTime(DateUtils.getNowDate());
        return sysWxGroupMapper.insertSysWxGroup(sysWxGroup);
    }

    /**
     * 修改wx群维护
     * 
     * @param sysWxGroup wx群维护
     * @return 结果
     */
    @Override
    public int updateSysWxGroup(SysWxGroup sysWxGroup) throws  Exception
    {
        sysWxGroup.setUpdateTime(DateUtils.getNowDate());
        //actionflag //功能标识 新增add  修改edit   新增成员 deleterson  addperson    新增备注 addtote  修改群等级 editlevel  加群addgroup  /addtote
        //成员编辑  需要记录起来
        if (sysWxGroup.getActionflag()!=null&&sysWxGroup.getActionflag().trim().equals("addperson")){
            //新增
            if (sysWxGroup.getMember()!=null&&(!sysWxGroup.getMember().trim().isEmpty())){
                String[] newMember=sysWxGroup.getMember().trim().split(",");
                List<String> needInserList=new ArrayList<>();
                List<String> allMemberList=new ArrayList<>();
                if (newMember.length>0){
                    SysWxGroup inner=    sysWxGroupMapper.selectSysWxGroupById(sysWxGroup.getId());
                    //之前已经有成员
                    if (inner.getMember()!=null&&!(inner.getMember().trim().isEmpty())){
                        String[] innerMember=inner.getMember().trim().split(",");
                        for (int i=0;i<innerMember.length;i++){
                            allMemberList.add(innerMember[i]);
                        }

                        for (int i=0;i<newMember.length;i++){
                            boolean isinsert=true;
                            for (int j=0;j<innerMember.length;j++){
                                if (innerMember[j].trim().equals(newMember[i].trim())){
                                    isinsert=false;
                                }
                            }
                            if (isinsert){
                                needInserList.add(newMember[i]);
                            }
                        }
                        allMemberList.addAll(needInserList);

                    }else {
                        for (int i=0;i<newMember.length;i++){
                            needInserList.add(newMember[i]);
                            allMemberList.add(newMember[i]);
                        }

                    }
                    //toto  将新增记录  插入表中
                    if (needInserList!=null&&needInserList.size()>0){
                        SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                        sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                        sysWxOperateRecord.setWxOpareteType(WxOperateType.ADD_PERSON);
                        sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                        sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                        sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                        sysWxOperateRecord.setRemark("添加成员,成员id为--"+String.join(",",needInserList));
                        sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
                    }

                    //设置新值
                    sysWxGroup.setMember(String.join(",",allMemberList));
                }

            }

        }
        else if (sysWxGroup.getActionflag()!=null&&sysWxGroup.getActionflag().trim().equals("deleterson")){
            //删除
            if (sysWxGroup.getMember()!=null&&!(sysWxGroup.getMember().trim().isEmpty())){
                String[] newMember=sysWxGroup.getMember().trim().split(",");
                List<String> needDeletList=new ArrayList<>();
                List<String> allMemberList=new ArrayList<>();
                if (newMember.length>0){
                    SysWxGroup inner=    sysWxGroupMapper.selectSysWxGroupById(sysWxGroup.getId());
                    //之前已经有成员 才校验是否删除
                    if (inner.getMember()!=null&&!(inner.getMember().trim().isEmpty())){
                        String[] innerMember=inner.getMember().trim().split(",");

                        for (int i=0;i<innerMember.length;i++){
                            boolean isDelete=false;
                            for (int j=0;j<newMember.length;j++){
                                if (newMember[j].trim().equals(innerMember[i].trim())){
                                    isDelete=true;
                                }
                            }
                            if (isDelete){
                                needDeletList.add(innerMember[i]);
                            }else {
                            allMemberList.add(innerMember[i]);//新集合
                           }
                        }

                    //toto  将新增记录  插入表中
                        if (needDeletList!=null&&needDeletList.size()>0){
                            //有删除的  并记录表中
                            SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                            sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                            sysWxOperateRecord.setWxOpareteType(WxOperateType.DELET_PERSON);
                            sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                            sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                            sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                            sysWxOperateRecord.setRemark("移除了成员,成员id为--"+String.join(",",needDeletList));
                            sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);

                            //将之前邀请记录也删掉---移除成员时，
                            for (int dd=0;dd<needDeletList.size();dd++){
                               String sql=" delete from sys_wxapply_record  where groupid="+sysWxGroup.getId()+" and member='"+needDeletList.get(dd)+"'";
                                jdbcTemplate.execute(sql);
                            }

                        }
                    }

                    //设置新值
                    sysWxGroup.setMember(String.join(",",allMemberList));
                }

            }
        }
        //加群操作---
        else if (sysWxGroup.getActionflag()!=null&&sysWxGroup.getActionflag().trim().equals("addgroup")){
           //判断 加群的值是否为空  为空则返回
            if (sysWxGroup.getGroupNumber()==null||sysWxGroup.getGroupNumber().trim().isEmpty()){
                throw new Exception("没有群编号的群，无法添加群成员");
            }

            SysWxapplyRecord sysWxapplyRecord=new SysWxapplyRecord();
            sysWxapplyRecord.setAttribute1(sysWxGroup.getAttribute4());
            sysWxapplyRecord.setAreamemo(sysWxGroup.getAreamemo());
            sysWxapplyRecord.setGroupid(sysWxGroup.getId());
            sysWxapplyRecord.setGroupNumber(sysWxGroup.getGroupNumber());
            sysWxapplyRecord.setGroupownerName(sysWxGroup.getGroupownerName());
            sysWxapplyRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
            sysWxapplyRecord.setJoinmemo(sysWxGroup.getJoinmemo());
            sysWxapplyRecord.setMember(sysWxGroup.getAttribute2());
            sysWxapplyRecord.setSubBrand(sysWxGroup.getSubBrand());
            sysWxapplyRecord.setStatus("1");
            sysWxapplyRecordMapper.insertSysWxapplyRecord(sysWxapplyRecord);

            //将该条记录标识改为 申请中
            sysWxGroup.setAttribute3("申请中");
            //最新申请时间改为当前
            sysWxGroup.setAttribute5(DateUtils.getNowDate());
        } //修改备注操作
        else if (sysWxGroup.getActionflag()!=null&&sysWxGroup.getActionflag().trim().equals("addtote")){
            SysWxGroup inner=    sysWxGroupMapper.selectSysWxGroupById(sysWxGroup.getId());
            //查询备注是否发生变化
            if (inner != null&&sysWxGroup!=null&&inner.getAreamemo()!=null&&sysWxGroup.getAreamemo()!=null
                    &&(!inner.getAreamemo().trim().equals(sysWxGroup.getAreamemo()))) {
                //群级别发生了变化
                SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                sysWxOperateRecord.setWxOpareteType(WxOperateType.TOTE_CHANGE);
                sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                sysWxOperateRecord.setRemark("地区备注由原来--"+inner.getAreamemo()+"改为---"+sysWxGroup.getAreamemo());
                sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
            }
            if (inner != null&&sysWxGroup!=null&&inner.getRemark()!=null&&sysWxGroup.getRemark()!=null
                    &&(!inner.getRemark().trim().equals(sysWxGroup.getRemark()))) {
                //群级别发生了变化
                SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                sysWxOperateRecord.setWxOpareteType(WxOperateType.TOTE_CHANGE);
                sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                sysWxOperateRecord.setRemark("备注由原来--"+inner.getAreamemo()+"改为---"+sysWxGroup.getAreamemo());
                sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
            }
            if (inner != null&&sysWxGroup!=null&&inner.getJoinmemo()!=null&&sysWxGroup.getJoinmemo()!=null
                    &&(!inner.getJoinmemo().trim().equals(sysWxGroup.getJoinmemo()))) {
                //群级别发生了变化
                SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                sysWxOperateRecord.setWxOpareteType(WxOperateType.TOTE_CHANGE);
                sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                sysWxOperateRecord.setRemark("加群备注由原来--"+inner.getAreamemo()+"改为---"+sysWxGroup.getAreamemo());
                sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
            }
        }
        else {
            SysWxGroup inner=    sysWxGroupMapper.selectSysWxGroupById(sysWxGroup.getId());
            //查询状态是否发生变化
            if (inner != null&&sysWxGroup!=null&&inner.getGruopLevel()!=null&&sysWxGroup.getGruopLevel()!=null
                    &&(inner.getGruopLevel()!=sysWxGroup.getGruopLevel())) {
                //群级别发生了变化
                SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                sysWxOperateRecord.setWxOpareteType(WxOperateType.CHANGE_LEVEL);
                sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                sysWxOperateRecord.setRemark("群活跃级别由原来状态id--"+inner.getGruopLevel()+"改为状态id---"+sysWxGroup.getGruopLevel());
                sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
            }
            if (inner != null&&sysWxGroup!=null&&inner.getIsCanjoin()!=null&&sysWxGroup.getIsCanjoin()!=null
                    &&(!inner.getIsCanjoin().equals(sysWxGroup.getIsCanjoin()))) {
                //群主微信是否可进入
                SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                sysWxOperateRecord.setWxOpareteType(WxOperateType.NOT_IN);
                sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                sysWxOperateRecord.setRemark("群主微信是否可进入由原来状态id---"+inner.getIsCanjoin()+"改为了状态id---"+sysWxGroup.getIsCanjoin());
                sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
            }
            if (inner != null&&sysWxGroup!=null&&inner.getStatus()!=null&&sysWxGroup.getStatus()!=null
                    &&(!inner.getStatus().equals(sysWxGroup.getStatus()) )) {
                //群是否能进入发生了变化
                SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                sysWxOperateRecord.setWxOpareteType(WxOperateType.GRUOP_STOP);
                sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                sysWxOperateRecord.setRemark("微信群是否停用由原来id-----"+inner.getStatus()+"改为了id----"+sysWxGroup.getStatus());
                sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
            }
            if (inner != null&&sysWxGroup!=null&&inner.getGroupownerPhone()!=null&&sysWxGroup.getGroupownerPhone()!=null
                    &&!(inner.getGroupownerPhone().equals(sysWxGroup.getGroupownerPhone()) )) {
                //群主微信变更
                SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                sysWxOperateRecord.setWxOpareteType(WxOperateType.OWNER_CHANGE);
                sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                sysWxOperateRecord.setRemark("微信群群主微信号由原来"+inner.getGroupownerPhone()+"改为了"+sysWxGroup.getGroupownerPhone());
                sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
            }   if (inner != null&&sysWxGroup!=null&&inner.getAttribute9()!=null&&sysWxGroup.getAttribute9()!=null
                    &&!(inner.getAttribute9().equals(sysWxGroup.getAttribute9()) )) {
                //群主微信变更
                SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
                sysWxOperateRecord.setWxgroupid(sysWxGroup.getId());
                sysWxOperateRecord.setWxOpareteType(WxOperateType.OWNER_CHANGE);
                sysWxOperateRecord.setGroupNumber(sysWxGroup.getGroupNumber());
                sysWxOperateRecord.setGroupownerPhone(sysWxGroup.getGroupownerPhone());
                sysWxOperateRecord.setGroupownerName(sysWxGroup.getGroupownerName());
                sysWxOperateRecord.setRemark("微信群是否可邀请由原来"+inner.getAttribute9()+"改为了"+sysWxGroup.getAttribute9());
                sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);
            }
        }
        //数据库中不记录邀请人，永远为空
        sysWxGroup.setAttribute4(null);
        return sysWxGroupMapper.updateSysWxGroup(sysWxGroup);
    }

    /**
     * 批量删除wx群维护
     * 
     * @param ids 需要删除的wx群维护主键
     * @return 结果
     */
    @Override
    public int deleteSysWxGroupByIds(Long[] ids)
    {
        return sysWxGroupMapper.deleteSysWxGroupByIds(ids);
    }

    /**
     * 删除wx群维护信息
     * 
     * @param id wx群维护主键
     * @return 结果
     */
    @Override
    public int deleteSysWxGroupById(Long id)
    {
        return sysWxGroupMapper.deleteSysWxGroupById(id);
    }
}
